package org.programus.nxj.util.txtimg;


/**
 * A 3x5 font implementation of TextImage. 
 * <p>Normally, we use this like below. 
 * <code><pre>
 * TextImage ti = new TextImage3x5(); 
 * Image message = ti.getImage("MESSAGE"); 
 * DisplayUtil.drawImageCenter(message, (LCD.SCREEN_HEIGHT >> 1), LCD.ROP_XOR); 
 * </pre></code>
 * </p>
 * @author Programus
 * @see TextImage
 */
public class TextImage3x5 extends TextImage {
	
	private final static int ASCII_OFFSET = ' '; 
	private final static int NUM_OFFSET = ' ' - '0'; 
	
	private final static byte[] UNKNOWN_DATA = {(byte)0xff, (byte)0xff, (byte)0xff}; 
	
	private final static byte[][] ASCII_DATA = {
		{0x00, 0x00, 0x00}, // space
		{0x00, 0x17, 0x00}, // !
		{0x03, 0x00, 0x03}, // "
		{0x1f, 0x0a, 0x1f}, // #
		{0x12, 0x1f, 0x09}, // $
		{0x19, 0x04, 0x13}, // %
		{0x0a, 0x15, 0x1d}, // &
		{0x00, 0x03, 0x00}, // '
		{0x00, 0x0e, 0x11}, // (
		{0x11, 0x0e, 0x00}, // )
		{0x15, 0x0e, 0x15}, // *
		{0x04, 0x1f, 0x04}, // +
		{0x10, 0x0c, 0x00}, // ,
		{0x04, 0x04, 0x04}, // -
		{0x00, 0x10, 0x00}, // .
		{0x18, 0x04, 0x03}, // /
		{0x1f, 0x11, 0x1f}, // 0
		{0x00, 0x02, 0x1f}, // 1
		{0x1d, 0x15, 0x17}, // 2
		{0x15, 0x15, 0x1f}, // 3
		{0x07, 0x04, 0x1f}, // 4
		{0x17, 0x15, 0x1d}, // 5
		{0x1f, 0x15, 0x1d}, // 6
		{0x01, 0x01, 0x1f}, // 7
		{0x1f, 0x15, 0x1f}, // 8
		{0x07, 0x05, 0x1f}, // 9
		{0x00, 0x0a, 0x00}, // :
		{0x10, 0x0a, 0x00}, // ;
		{0x04, 0x0a, 0x11}, // <
		{0x0a, 0x0a, 0x0a}, // =
		{0x11, 0x0a, 0x04}, // >
		{0x01, 0x15, 0x02}, // ?
		
		{0x1e, 0x1b, 0x16}, // @
		{0x1e, 0x09, 0x1e}, // A
		{0x1f, 0x15, 0x0a}, // B
		{0x0e, 0x11, 0x11}, // C
		{0x1f, 0x11, 0x0e}, // D
		{0x1f, 0x15, 0x15}, // E
		{0x1f, 0x05, 0x05}, // F
		{0x0e, 0x15, 0x1d}, // G
		{0x1f, 0x04, 0x1f}, // H
		{0x11, 0x1f, 0x11}, // I
		{0x08, 0x11, 0x0f}, // J
		{0x1f, 0x0a, 0x11}, // K
		{0x1f, 0x10, 0x10}, // L
		{0x1f, 0x02, 0x1f}, // M
		{0x1f, 0x01, 0x1f}, // N
		{0x0e, 0x11, 0x0e}, // O
		{0x1f, 0x05, 0x07}, // P
		{0x0f, 0x19, 0x17}, // Q
		{0x1f, 0x0d, 0x17}, // R
		{0x12, 0x15, 0x09}, // S
		{0x01, 0x1f, 0x01}, // T
		{0x1f, 0x10, 0x1f}, // U
		{0x0f, 0x10, 0x0f}, // V
		{0x1f, 0x08, 0x1f}, // W
		{0x1b, 0x04, 0x1b}, // X
		{0x03, 0x1c, 0x03}, // Y
		{0x19, 0x15, 0x13}, // Z
		{0x00, 0x1f, 0x11}, // [
		{0x03, 0x04, 0x18}, // \
		{0x11, 0x1f, 0x00}, // ]
		{0x02, 0x01, 0x02}, // ^
		{0x10, 0x10, 0x10}, // _
		
		{0x01, 0x02, 0x00}, // `
		{0x1e, 0x09, 0x1e}, // A
		{0x1f, 0x15, 0x0a}, // B
		{0x0e, 0x11, 0x11}, // C
		{0x1f, 0x11, 0x0e}, // D
		{0x1f, 0x15, 0x15}, // E
		{0x1f, 0x05, 0x05}, // F
		{0x0e, 0x15, 0x1d}, // G
		{0x1f, 0x04, 0x1f}, // H
		{0x11, 0x1f, 0x11}, // I
		{0x08, 0x11, 0x0f}, // J
		{0x1f, 0x0a, 0x11}, // K
		{0x1f, 0x10, 0x10}, // L
		{0x1f, 0x02, 0x1f}, // M
		{0x1f, 0x01, 0x1f}, // N
		{0x0e, 0x11, 0x0e}, // O
		{0x1f, 0x05, 0x07}, // P
		{0x0f, 0x19, 0x17}, // Q
		{0x1f, 0x0d, 0x17}, // R
		{0x12, 0x15, 0x09}, // S
		{0x01, 0x1f, 0x01}, // T
		{0x1f, 0x10, 0x1f}, // U
		{0x0f, 0x10, 0x0f}, // V
		{0x1f, 0x08, 0x1f}, // W
		{0x1b, 0x04, 0x1b}, // X
		{0x03, 0x1c, 0x03}, // Y
		{0x19, 0x15, 0x13}, // Z
		{0x04, 0x1f, 0x11}, // {
		{0x00, 0x1f, 0x00}, // |
		{0x11, 0x1f, 0x04}, // }
		{0x02, 0x03, 0x01}, // ~
	}; 

	@Override
	protected int getCharHeight() {
		return 5;
	}

	@Override
	protected int getCharWidth() {
		return 3;
	}

	@Override
	protected byte[] getImageData(int i) {
		if (i > 9) {
			i %= 10; 
		} else if (i < 0) {
			i = 0; 
		}
		int index = i - NUM_OFFSET; 
		return ASCII_DATA[index];
	}

	@Override
	protected byte[] getImageData(char c) {
		int index = c - ASCII_OFFSET; 
		if (index < 0 || index >= ASCII_DATA.length) {
			return UNKNOWN_DATA; 
		}
		
		return ASCII_DATA[index]; 
	}
}
